//
//  MovieViewController.m
//  UI_豆瓣影院
//
//  Created by dllo on 16/1/9.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "MovieViewController.h"
#import "AAALHXDateHandle.h"
#import "Second1ViewController.h"
@interface MovieViewController ()<UITableViewDataSource, UITableViewDelegate>
@property (nonatomic, retain) UITableView *tableView;

@property (nonatomic, retain) NSMutableArray *arr1;
@end

@implementation MovieViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationItem.title = @"电影列表";
    
    
    // 创建tableView
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, 375, 667) style:UITableViewStylePlain];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.separatorColor = [UIColor cyanColor];
    _tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine ;
    [self.view addSubview:_tableView];
    [_tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"];
    [_tableView release];
    self.navigationItem.rightBarButtonItem = self.editButtonItem;

    
    
}
// 行的个数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return _arr.count;
}

// 行的高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    return 50;
}

// 编辑
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    
    cell.textLabel.text =_arr[indexPath.row];
    cell.backgroundColor = [UIColor whiteColor];
    return cell;
}
// 重写系统的编辑按钮的触发方法
- (void)setEditing:(BOOL)editing animated:(BOOL)animated {
    
    [super setEditing:editing animated:animated];
    [_tableView setEditing:editing animated:animated];
    
}
// 修改删除按钮的文字
- (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath {
    return @"取消收藏";
}

// 实现编辑功能
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        
        
        
       [[AAALHXDateHandle sharedDateBaseHandle]deleteModel1:_arr[indexPath.row]];
        
        [_arr removeObjectAtIndex:indexPath.row];
        
        [_tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationLeft];
        
        
    } else {
        
    }
    
    
}

// 点击方法

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [self dataHounde];
    
    NSString *str = _arr[indexPath.row];
    CinemaModel *model = [CinemaModel cinemaModel];
    model.title = str;
    Second1ViewController *vc = [[Second1ViewController alloc] init];
    
    // 回传到Second1
    for (CinemaModel *model1 in _arr1) {
        if ([model1.title isEqualToString:model.title]) {
            
            vc.model = model1;
            
            [self.navigationController pushViewController:vc animated:YES];
        }
    }
 
    
    
    
    
    
}
- (void)dataHounde {
    NSString *str = @"http://api.douban.com/v2/movie/nowplaying?app_name=doubanmovie&client=e:iPhone4,1|y:iPhoneOS_6.1|s:mobile|f:doubanmovie_2|v:3.3.1|m:PP_market|udid:aa1b815b8a4d1e961347304e74b9f9593d95e1c5&alt=json&version=2&start=1&city=北京&apikey=0df993c66c0c636e29ecbb5344252a4a";
    NSString *newStr = [str stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:newStr]];
    NSError *error = nil;
    id result = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:&error];
    
    NSMutableDictionary *dic1 = result;
    NSArray *arr = dic1[@"entries"];
    self.arr1 = [NSMutableArray array];
    for (NSMutableDictionary *dic in arr) {
        CinemaModel *model = [CinemaModel cinemaModel];
        [model setValuesForKeysWithDictionary:dic];
        [_arr1 addObject:model];
        
        
    }
    
}




- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
